<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>插入一个文档</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.collection.html">获取集合实例</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.tutorial.findone.html">使用 MongoCollection::findOne 方法</a></div>
 <div class="up"><a href="mongo.tutorial.html">教程</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongo.tutorial.insert" class="section">
   <h2 class="title">插入一个文档</h2>
   <p class="para">
    关联数组是能插入集合的最基本结构。
    一般的“文档”结构可能是这样：
   </p>
   <div class="example" id="mongo.tutorial.insert-data-example">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$doc&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"name"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"MongoDB"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"type"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"database"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"count"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"info"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;(object)array(&nbsp;</span><span style="color: #DD0000">"x"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">203</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"y"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">102</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"versions"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">"0.9.7"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"0.9.8"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"0.9.9"</span><span style="color: #007700">)<br />);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   <p class="para">
    注意：你可以嵌套数组、对象。驱动会把关联数组保存为
    js对象，从0开始的连续数字下标数组保存为
    js数组，不从0开始或有间断的（如0,1,4,5）数组保存为
    js对象。
    译注：即：只有从0开始的连续数字下标数组保存为数组，其他复杂类型均为对象。
   </p>
   <p class="para">
    要插入这个文档，使用 <span class="function"><a href="mongocollection.insert.html" class="function">MongoCollection::insert()</a></span> 方法：
   </p>
   <div class="example" id="mongo.tutorial.insert-example-2">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$connection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$collection&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$connection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">database</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">collectionName</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$collection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$doc&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   <div class="section" id="mongo.tutorial.insert.seealso">
    <h2 class="title">参见</h2>
    <p class="para">
     <span class="function"><a href="mongocollection.insert.html" class="function">MongoCollection::insert()</a></span>
     方法的文档中有对插入数据的详细说明。
    </p>
   </div>
  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.collection.html">获取集合实例</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.tutorial.findone.html">使用 MongoCollection::findOne 方法</a></div>
 <div class="up"><a href="mongo.tutorial.html">教程</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
